package com.ruoyi.api.api;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripperByArea;

import java.awt.geom.Rectangle2D;
import java.io.File;

/**
 * @author Bella
 * @name Scratch
 * @description
 * @date 2025/6/11 16:58
 */
class Scratch {
    public static void main(String[] args) throws Exception {
        String path = "C:\\Users\\寻觅\\Desktop\\test.pdf"; // PDF 文件路径

        extractAllPages(path);
    }

    public static void extractAllPages(String pdfPath) throws Exception {
        PDFTextStripperByArea regionStripper = new PDFTextStripperByArea();
        regionStripper.setSortByPosition(true);

        // 假设页面尺寸 A4，原点在左下。坐标根据实际调试得出
        // 比如「投资者编号」在(100, 700) 到 (300, 720) 区域
        regionStripper.addRegion("investorId", new Rectangle2D.Float(100, 240, 270, 10));
        regionStripper.addRegion("investorName", new Rectangle2D.Float(100, 250, 270, 10));
        regionStripper.addRegion("explain", new Rectangle2D.Float(10, 290, 270, 10));
        regionStripper.addRegion("endDate", new Rectangle2D.Float(137, 840-461, 90, 13));
        regionStripper.addRegion("units", new Rectangle2D.Float(256, 840-461, 90, 13));
        regionStripper.addRegion("nav", new Rectangle2D.Float(335, 840-461, 90, 13));
        regionStripper.addRegion("marketValue", new Rectangle2D.Float(495, 840-461, 90, 13));
        regionStripper.addRegion("valueChangePct", new Rectangle2D.Float(504, 840-448, 80, 13));
        regionStripper.addRegion("email", new Rectangle2D.Float(380, 130, 132, 15));

        try (PDDocument document = PDDocument.load(new File(pdfPath))) {
            int pageCount = document.getNumberOfPages();

            for (int i = 0; i < pageCount; i++) {
                regionStripper.extractRegions(document.getPage(i));
                System.out.println("投资者编号: " + regionStripper.getTextForRegion("investorId"));
                System.out.println("投资者姓名: " + regionStripper.getTextForRegion("investorName"));
                System.out.println("股份类别说明: " + regionStripper.getTextForRegion("explain"));
                System.out.println("期末持仓日期: " + regionStripper.getTextForRegion("endDate"));
                System.out.println("期末份额: " + regionStripper.getTextForRegion("units"));
                System.out.println("每股估值: " + regionStripper.getTextForRegion("nav"));
                System.out.println("市值: " + regionStripper.getTextForRegion("marketValue"));
                System.out.println("估值变动百分比: " + regionStripper.getTextForRegion("valueChangePct"));
                System.out.println("电邮: " + regionStripper.getTextForRegion("email"));
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
